﻿using System;
using System.Configuration;
using Navigant.Infrastructure.Cases;
using Navigant.Infrastructure.RepositoryFramework.Configuration;

namespace Navigant.Infrastructure.RepositoryFramework
{
    public static class CaseRepositoryFactory
    {
        private static ICaseRepository caseRepository;

        public static ICaseRepository GetCaseRepository()
        {
            // See if the ICaseRepository instance was already created
            if (CaseRepositoryFactory.caseRepository == null)
            {
                // It was not created, so build it now
                RepositorySettings settings =
                    (RepositorySettings)ConfigurationManager.GetSection(
                    RepositoryMappingConstants.RepositoryMappingsConfigurationSectionName);

                // Get the type to be created
                Type repositoryType =
                    Type.GetType(
                    settings.RepositoryMappings["ICaseRepository"].RepositoryFullTypeName);

                // Create the repository, and cast it to the 
                // IReferenceDataRepository interface
                CaseRepositoryFactory.caseRepository =
                    Activator.CreateInstance(repositoryType) as ICaseRepository;

            }

            return CaseRepositoryFactory.caseRepository;
        }
    }
}
